﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using BitPortal.Models;
using Ext.Net;
using BitPortal.Business;
namespace BitPortal.Admin.Account.Admin
{
    public partial class Default : Controls.BaseAdminPage
    {
        protected override void OnInit(EventArgs e)
        {
            PermissionBLL.CheckRoleThrowException(role: "AccountView", useExceptionHandler: true);
            base.OnInit(e);
            (this.AdminAddEdit1 as Controls.BaseAdminControl).ReloadControls.Add(this.GridPanel1);
            if (string.IsNullOrEmpty(Request.QueryString["Role"]))
            {
                cbRole.SelectedIndex = 0;
            }
            else
            {
                cbRole.SelectedItem.Value = Request.QueryString["Role"];
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
        {
            FilterConditions fc = null;
            string s = e.Parameters[this.GridFilters1.ParamPrefix];
            if (!string.IsNullOrEmpty(s))
            {
                fc = new FilterConditions(s);
            }

            List<MembershipUser> data = new List<MembershipUser>();
            if (string.IsNullOrEmpty(Request.QueryString["Role"]) || Request.QueryString["Role"].Equals("All"))
            {
                data = UserBLL.GetAll(Roles.IsUserInRole("RootAdmin"));
            }
            else
            {
                data = UserBLL.GetByRole(Request.QueryString["Role"], Roles.IsUserInRole("RootAdmin"));
            }
            Store1.DataSource = ExtNet.GirdData.Filter<MembershipUser>(data, fc, e);
        }

        protected void BindData()
        {
            (this.Master as AdminFunction).MasterResourceManager.AddScript("Ext.getCmp('" + GridPanel1.ClientID + "').getStore().reload();");
        }

        //[DirectMethod]
        //public void DoSomething(string id)
        //{
        //    AdminAddEdit1.BindData(id);
        //}

        protected void btnAdd_Click(object sender, DirectEventArgs e)
        {
            AdminAddEdit1.BindData(null);
            Window1.Show();
        }

        protected void btnEdit_Click(object sender, DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
            if (sm.SelectedRows.Count > 0)
            {
                AdminAddEdit1.BindData(sm.SelectedRows[0].RecordID);
                Window1.Show();
            }
        }

        protected void btnLog_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("AccountEdit");

                RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
                int record = UserBLL.LockUser(sm.SelectedRows.Select(ent => ent.RecordID).ToArray());
                if (record > 0)
                {
                    ShowNotification("Khóa " + record + " người dùng thành công!");
                    BindData();
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

        protected void btnUnLog_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("AccountEdit");
                RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
                int record = 0;
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    MembershipUser user = Membership.GetUser(row.RecordID);
                    if (user != null)
                    {
                        if (user.UnlockUser()) record++;
                    }
                }
                if (record > 0)
                {
                    ShowNotification("Bỏ khóa " + record + " người dùng thành công!");
                    BindData();
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

        protected void btnDelete_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("AccountDelete");
                RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
                int record = 0;
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    MembershipUser user = Membership.GetUser(row.RecordID);
                    if (user != null)
                    {
                        if (Membership.DeleteUser(row.RecordID, true)) record++;
                    }

                }
                if (record > 0)
                {
                    ShowNotification("Xóa " + record + " người dùng thành công!");
                    BindData();
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

    }
}